# set project name
project(topcvt)
set(PROJECT_VERSION "0.0.1")

# set cmake mini version
cmake_minimum_required(VERSION 2.8)

if(CMAKE_VERSION VERSION_GREATER 3)
	cmake_policy(SET CMP0054 NEW)
endif()
add_definitions(-D_TOPCVT_)

# Generate version header file
configure_file(
  ${PROJECT_SOURCE_DIR}/config.h.in
  ${PROJECT_SOURCE_DIR}/src/topcvt_version.h
  )

# print build info
message(STATUS "CMAKE_SYSTEM_NAME     " ${CMAKE_SYSTEM_NAME})
message(STATUS "PROJECT_SOURCE_DIR    " ${PROJECT_SOURCE_DIR})
message(STATUS "PROJECT_BINARY_DIR    " ${PROJECT_BINARY_DIR})
message(STATUS "CMAKE_BINARY_DIR      " ${CMAKE_BINARY_DIR})
message(STATUS "CMAKE_CXX_COMPILER_ID " ${CMAKE_CXX_COMPILER_ID})
message(STATUS "CMAKE_C_COMPILER:     " ${CMAKE_C_COMPILER})
message(STATUS "CMAKE_CXX_COMPILER:   " ${CMAKE_CXX_COMPILER})

# set cmake install prefix
#set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR})
set(CMAKE_INSTALL_PREFIX $ENV{HOME})

# set c++11 options
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# set Build Type (Debug/Release)
if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE "Release")
endif()
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
	add_compile_options(-g -std=c++0x )
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ldl -lpthread")
	add_compile_options(-fPIC)
endif()

# check build type 
if(CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
	add_definitions(-D_DEBUG_)
	if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
		# using GCC
		# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -g ")
		add_compile_options(-O0)		
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -Wall -pedantic -Wshadow -Wextra ")
	elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
		# using regular Clang or AppleClang
		add_compile_options(-O0)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -Wall -Wno-invalid-source-encoding -Wshadow -Wshorten-64-to-32 ")
	elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")		
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -D_CRT_SECURE_NO_WARNINGS=1 /Zi /Od /Ob0 /RTC1 /MDd ")
	endif()	
	#set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/debug)
	message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
elseif(CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
	add_definitions(-D_RELEASE_)
	if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
		# using GCC
		add_compile_options(-O3)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic -Wno-unused-value  -Wno-long-long ")	
	elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
		# using regular Clang or AppleClang
		add_compile_options(-O3)		
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic -Wno-unused-value -Wno-invalid-source-encoding -Wno-parentheses ")
	elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")		
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -D_CRT_SECURE_NO_WARNINGS=1 /Od /Ob0 /MDd ")		
	endif()		
	#set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/release)
	message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
endif()

if("${CMAKE_OS_TYPE}" STREQUAL "windows")
    ADD_DEFINITIONS(-DTOPCVT_EXPORTS)
endif()

# Ensure that all executables and libraries end up in the same directory. Actual
# files might end up in a configuration subdirectory.
if(NOT EXECUTABLE_OUTPUT_PATH)
	set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin CACHE INTERNAL
		   "Single output directory for building all executables.")
endif()
if(NOT LIBRARY_OUTPUT_PATH)
	set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib CACHE INTERNAL
		   "Single output directory for building all libraries.")
endif()
if(NOT ARCHIVE_OUTPUT_PATH)
	set(ARCHIVE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib CACHE INTERNAL
		   "Single output directory for building all archives.")
endif()
if(NOT INCLUDE_OUTPUT_PATH)
	set(INCLUDE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/include CACHE INTERNAL
		   "Single output directory for building all includes.")
endif() 

# build CMAKE_INSTALL_PREFIX
if(NOT EXECUTABLE_INSTALL_PATH)
	set(EXECUTABLE_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/bin CACHE INTERNAL
		   "Single install directory for building all executables.")
endif()
if(NOT LIBRARY_INSTALL_PATH)
	set(LIBRARY_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/lib CACHE INTERNAL
		   "Single install directory for building all libraries.")
endif()
if(NOT ARCHIVE_INSTALL_PATH)
	set(ARCHIVE_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/lib CACHE INTERNAL
		   "Single install directory for building all archives.")
endif()
if(NOT INCLUDE_INSTALL_PATH)
	set(INCLUDE_INSTALL_PATH ${CMAKE_INSTALL_PREFIX}/include CACHE INTERNAL
		   "Single install directory for building all includes.")
endif() 

# set PERMISSIONS
set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
     OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ )

set(CMAKE_INSTALL_DEFAULT_LIBRARY_PERMISSIONS
	OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ )   

set(CMAKE_INSTALL_DEFAULT_EXECUTABLE_PERMISSIONS
	OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ WORLD_READ ) 

if(NOT SUPPORT_XERCESC)
option(SUPPORT_XERCESC    "ENABLE SUPPORT XERCESC"    OFF  )
endif()

if(NOT ENABLE_USE_LOGCPLUS)
option(ENABLE_USE_LOGCPLUS  "ENABLE_USE_LOGCPLUS"  ON  )
endif()

if(SUPPORT_XERCESC) 
	add_definitions(-DSUPPORT_XERCESC)
endif()     

if(ENABLE_USE_LOGCPLUS) 
	add_definitions(-DENABLE_USE_LOGCPLUS)
endif()   

message(STATUS "ENABLE_USE_LOGCPLUS " ${ENABLE_USE_LOGCPLUS})
message(STATUS "ENABLE_SUPPORT_XERCESC " ${SUPPORT_XERCESC})

# add_subdirectory
add_subdirectory(src)
add_subdirectory(tools)
add_subdirectory(test)